package cn.ppms.controller;

import cn.ppms.pojo.User;
import cn.ppms.service.user.UserService;
import cn.ppms.service.user.UserServiceImpl;
import cn.ppms.tools.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@Controller
public class LoginController   {
    @RequestMapping("/login")
    protected String login(HttpServletRequest request ) {
        System.out.println("login ============ " );
        //获取用户名和密码
        String userCode = request.getParameter("userCode");
        String userPassword = request.getParameter("userPassword");
        //调用service方法，进行用户匹配
        UserService userService = new UserServiceImpl();
        User user = userService.login(userCode,userPassword);
        if(null != user){//登录成功
            //放入session
            request.getSession().setAttribute(Constants.USER_SESSION, user);
            //页面跳转（frame.jsp）
            return "frame";
        }else{
            //页面跳转（login.jsp）带出提示信息--转发
            request.setAttribute("error", "用户名或密码错误");
            return "../login";
        }
    }

    /**
     * 访问主页 : 可以是 登录之后 进行 跳转
     */
    @RequestMapping("/indexController")
    public String index( Map<String , Object > requestData ){
        return "login" ;
    }
}
